import { Brands } from './brand';
let _Brands = Brands;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import BrandMock from './mallm/pub/brandMock';
 * BrandMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/brand/listKey').reply(config => {
      let params = config.params;
      let mockBrands = _Brands.filter(brand => {
      	if(params.id||params.id==''){ if( brand.id.indexOf(params.id)>=0) return true;}
      	if(params.name||params.name==''){ if( brand.name.indexOf(params.name)>=0) return true;}
      	if(params.listPicUrl||params.listPicUrl==''){ if( brand.listPicUrl.indexOf(params.listPicUrl)>=0) return true;}
      	if(params.simpleDesc||params.simpleDesc==''){ if( brand.simpleDesc.indexOf(params.simpleDesc)>=0) return true;}
      	if(params.picUrl||params.picUrl==''){ if( brand.picUrl.indexOf(params.picUrl)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( brand.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.isShow||params.isShow==''){ if( brand.isShow.indexOf(params.isShow)>=0) return true;}
      	if(params.floorPrice||params.floorPrice==''){ if( brand.floorPrice.indexOf(params.floorPrice)>=0) return true;}
      	if(params.appListPicUrl||params.appListPicUrl==''){ if( brand.appListPicUrl.indexOf(params.appListPicUrl)>=0) return true;}
      	if(params.isNew||params.isNew==''){ if( brand.isNew.indexOf(params.isNew)>=0) return true;}
      	if(params.newPicUrl||params.newPicUrl==''){ if( brand.newPicUrl.indexOf(params.newPicUrl)>=0) return true;}
      	if(params.newSortOrder||params.newSortOrder==''){ if( brand.newSortOrder.indexOf(params.newSortOrder)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( brand.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockBrands
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/brand/list').reply(config => {
      let params = config.params;
      let mockBrands = _Brands.filter(brand => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(brand.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(brand.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.listPicUrl||params.listPicUrl==''){if(brand.listPicUrl.indexOf(params.listPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.simpleDesc||params.simpleDesc==''){if(brand.simpleDesc.indexOf(params.simpleDesc)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.picUrl||params.picUrl==''){if(brand.picUrl.indexOf(params.picUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(brand.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isShow||params.isShow==''){if(brand.isShow.indexOf(params.isShow)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.floorPrice||params.floorPrice==''){if(brand.floorPrice.indexOf(params.floorPrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.appListPicUrl||params.appListPicUrl==''){if(brand.appListPicUrl.indexOf(params.appListPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isNew||params.isNew==''){if(brand.isNew.indexOf(params.isNew)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.newPicUrl||params.newPicUrl==''){if(brand.newPicUrl.indexOf(params.newPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.newSortOrder||params.newSortOrder==''){if(brand.newSortOrder.indexOf(params.newSortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(brand.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockBrands
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockBrands.length;
      mockBrands = mockBrands.filter((brand, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockBrands,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/brand/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Brands = _Brands.filter(brand => brand.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/brand/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Brands = _Brands.filter(brand => !params.includes(brand.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/brand/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,listPicUrl,simpleDesc,picUrl,sortOrder,isShow,floorPrice,appListPicUrl,isNew,newPicUrl,newSortOrder,deptid } = params;
      _Brands.some(brand => {
        if (brand.id === id) {
			brand.id = id;
			brand.name = name;
			brand.listPicUrl = listPicUrl;
			brand.simpleDesc = simpleDesc;
			brand.picUrl = picUrl;
			brand.sortOrder = sortOrder;
			brand.isShow = isShow;
			brand.floorPrice = floorPrice;
			brand.appListPicUrl = appListPicUrl;
			brand.isNew = isNew;
			brand.newPicUrl = newPicUrl;
			brand.newSortOrder = newSortOrder;
			brand.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/brand/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,listPicUrl,simpleDesc,picUrl,sortOrder,isShow,floorPrice,appListPicUrl,isNew,newPicUrl,newSortOrder,deptid } = params;
      _Brands.push({
		id:id,
		name:name,
		listPicUrl:listPicUrl,
		simpleDesc:simpleDesc,
		picUrl:picUrl,
		sortOrder:sortOrder,
		isShow:isShow,
		floorPrice:floorPrice,
		appListPicUrl:appListPicUrl,
		isNew:isNew,
		newPicUrl:newPicUrl,
		newSortOrder:newSortOrder,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};